import 'dart:async';

import 'package:flutter/services.dart';

class TP809 {
  static const MethodChannel _channel = MethodChannel('tp_809');

  ///連接打印機
  static Future<void> connectPrinter({required String ipAddress, required String port}) async {
    await _channel.invokeMethod('connectPrinter', <String, String>{
      'ipAddress': ipAddress,
      'port': port,
    });
  }

  ///斷開打印機
  static Future<void> disconnectPrinter() async {
    await _channel.invokeMethod('disconnectPrinter', <String, String>{});
  }

  ///打印機是否已連接
  static Future<bool> isConnect() async {
    return await _channel.invokeMethod('isConnect', <String, String>{});
  }

  ///打印文本
  static Future<void> printText({required String text}) async {
    await _channel.invokeMethod('printText', <String, dynamic>{
      'text': text,
    });
  }

  ///打印圖片
  /// 1 inch = size:192
  /// 2 inch = size:384
  static Future<void> printImage({required String filePath, int size = 576}) async {
    await _channel.invokeMethod('printImage', <String, dynamic>{
      'filePath': filePath,
      'size': size,
    });
  }


  ///打開錢箱 mode：
  /// 0 - 打開1號錢箱
  /// 1 - 打開2號錢箱
  /// 2 - 打開1號和2號錢箱
  static Future<void> openCashDrawer({int mode = 0}) async {
    await _channel.invokeMethod('openCashDrawer', <String, dynamic>{
      'mode': mode,
    });
  }
}

class HT300 {
  static const MethodChannel _channel = MethodChannel('ht_300');

  ///連接打印機
  static Future<void> connectPrinter({required String ipAddress, required String port}) async {
    await _channel.invokeMethod('connectPrinter', <String, String>{
      'ipAddress': ipAddress,
      'port': port,
    });
  }

  ///斷開打印機
  static Future<void> disconnectPrinter() async {
    await _channel.invokeMethod('disconnectPrinter', <String, String>{});
  }

  ///打印機是否已連接
  static Future<bool> isConnect() async {
    return  await _channel.invokeMethod('isConnect', <String, String>{});
  }

  ///打印文本
  static Future<void> printText({required String text}) async {
    await _channel.invokeMethod('printText', <String, dynamic>{
      'text': text,
    });
  }

  ///打印圖
  static Future<void> printImage({required String filePath, int size = 1100}) async {
    await _channel.invokeMethod('printImage', <String, dynamic>{
      'filePath': filePath,
      'size': size,
    });
  }

  ///设置坐标
  static Future<void> setXY({required int x, required int y}) async {
    await _channel.invokeMethod('setXY', <String, dynamic>{
      'x': x,
      'y': y,
    });
  }

  ///设置标签原点位置
  static Future<void> LH({required int x, required int y}) async {
    await _channel.invokeMethod('LH', <String, dynamic>{
      'x': x,
      'y': y,
    });
  }
}
